//
//  ShopHotTagCell.m
//  童伙妈妈
//
//  Created by chengfengjie on 2016/10/11.
//  Copyright © 2016年 Liwy. All rights reserved.
//

#import "ShopHotTagCell.h"

@interface ShopHotTagCell () {
    NSArray *_dataSource;
}

@end

@implementation ShopHotTagCell

- (instancetype)initWithDataSource:(NSArray *)dataSource {
    self = [super init];
    if (self) {
        _dataSource = dataSource;
        UILabel * label = [[UILabel alloc] init];
        label.text = @"热门标签";
        label.font = [UIFont systemFontOfSize:20];
        label.textColor = [UIColor blackColor];
        [self addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@15);
            make.top.equalTo(@15);
        }];
        
        UIView * line = [[UIView alloc] init];
        line.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
        [self addSubview:line];
        
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@15);
            make.right.equalTo(@(-15));
            make.top.equalTo(label.mas_bottom).offset(5);
            make.height.equalTo(@(0.7));
        }];
        
        CGRect frame = CGRectMake(15, 50, 0, 0);
        for (int i = 0; i < dataSource.count; i++) {
            NSString *title = dataSource[i][@"description"];
            NSDictionary * attirbutes = @{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:[UIColor darkTextColor]};
            NSAttributedString * text = [[NSAttributedString alloc] initWithString:title attributes:attirbutes];
            CGSize size = [title boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:attirbutes context:nil].size;
            frame.size.width = size.width + 20;
            frame.size.height = 35;
            
            if (frame.origin.x + frame.size.width > kwidth - 15) {
                frame.origin.x = 15;
                frame.origin.y = frame.origin.y + 45;
            }
            
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = frame;
            [btn setAttributedTitle:text forState:UIControlStateNormal];
            btn.backgroundColor = [UIColor clearColor];
            btn.layer.cornerRadius = 5;
            btn.layer.borderColor = [UIColor colorWithWhite:0.75 alpha:0.8].CGColor;
            btn.layer.borderWidth = 1;
            btn.tag = 100 + i;
            [btn addTarget:self action:@selector(on_btn:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];
            
            self.cellHeight = CGRectGetMaxY(btn.frame) + 20;
            
            frame.origin.x = frame.origin.x + frame.size.width + 15;
        }
    }
    return self;
}

+ (CGFloat)cellHeightWithDataSource:(NSArray *)dataSource {
    ShopHotTagCell * cell = [[ShopHotTagCell alloc] initWithDataSource:dataSource];
    return cell.cellHeight;
}

- (void)on_btn:(UIButton *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(hotTagDidSelectedWithTagName:)]) {
        [self.delegate hotTagDidSelectedWithTagName:sender.tag - 100];
       
    }
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}



@end
